package edu.ptu.communityplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.ptu.communityplatform.domain.entity.Car;
import edu.ptu.communityplatform.domain.entity.Person;
import edu.ptu.communityplatform.domain.vos.PersonInfoVo;
import edu.ptu.communityplatform.domain.vos.PersonPageVo;
import edu.ptu.communityplatform.mapper.CarMapper;
import edu.ptu.communityplatform.mapper.PersonMapper;
import edu.ptu.communityplatform.service.IPersonService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @BelongsProject: communityPlatform
 * @BelongsPackage: edu.ptu.communityplatform.service.impl
 * @Author: HYJ
 * @CreateTime: 2025-04-05  03:02
 * @Description: TODO
 * @Version: 1.0
 */
@Service("PersonService")
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements IPersonService {

    @Resource
    private PersonMapper personMapper;

    @Resource
    private CarMapper carMapper;

    /**
     * 根据ID获取人员姓名
     *
     * @param personId 人员ID
     * @return 人员姓名
     */
    @Override
    public String getPersonNameById(Long personId) {
        return personMapper.getPersonNameById(personId);
    }

    /**
     * 根据ID获取人员手机号
     *
     * @param personId 人员ID
     * @return 人员手机号
     */
    @Override
    public String getPhoneNumberById(Long personId) {
        return personMapper.getPhoneNumberById(personId);
    }

    /**
     * 分页查询人员信息
     *
     * @param pageNum    页码
     * @param pageSize   每页大小
     * @param plotId     小区ID
     * @param personName 人员姓名
     * @param roleId     角色ID
     * @return 人员分页信息
     */
    @Override
    public IPage<Person> getPersonPage(Integer pageNum, Integer pageSize, Long plotId, String personName, Integer roleId) {
        // 分页查询
        Page<Person> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<Person> wrapper = new LambdaQueryWrapper<>();
        // 查询条件
        wrapper.eq(Person::getPlotId, plotId).like(StringUtils.isNotBlank(personName), Person::getPersonName, personName).eq(roleId != null, Person::getRoleId, roleId).orderByDesc(Person::getCreateTime);

        return this.page(page, wrapper);
    }

    @Transactional
    @Override
    public boolean removeById(Long personId) {
        // 检查是否存在关联的车辆
        if (carMapper.selectById(personId) != null) {
            carMapper.delete(new LambdaQueryWrapper<Car>()
                    .eq(Car::getPersonId, personId));
        }
        return super.removeById(personId);
    }

    /**
     * 根据人员ID获取小区ID
     *
     * @param personId 人员ID
     * @return 小区ID
     */
    @Override
    public Long getPlotIdByPersonId(Long personId) {
        return personMapper.getPlotIdByPersonId(personId);
    }


    @Override
    public IPage<PersonInfoVo> getPersonInfoPage(Integer pageNum, Integer pageSize, Long plotId, String personName) {
        // 分页查询
        Page<Person> page = new Page<>(pageNum, pageSize);
        return personMapper.getPage(page, plotId, personName);
    }
}
